package com.umeox.auto_light

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import com.umeox.sdk_ring.RingScanCallback
import com.umeox.sdk_ring.UMScanResult
import com.umeox.sdk_ring.scan.RingDirectScanner

object DeviceSearchManager {

    private val handler = Handler(Looper.getMainLooper())

    private val addressList = arrayListOf<String>()

    @SuppressLint("MissingPermission")
    private val call: RingScanCallback = object : RingScanCallback {
        override fun onScanResult(result: UMScanResult) {
            if (addressList.contains(result.device.address)) {
                return
            }
            if (!TextUtils.isEmpty(result.device.name) && result.rssi > -50) {
                addressList.add(result.device.address)
                scannerManager.stopScan()
                val device = Device()
                device.name = result.device.name
                device.mac = result.device.address
                device.rssi = result.rssi
                DeviceListManager.addDevice(device)
            }
        }
    }

    private val scannerManager = RingDirectScanner(this, call, arrayListOf("Zikr Ring Noor","Zikr Flex Advance","Zikr Ring Noor 2"))

    fun startScanTask() {
        scannerManager.startScan()
        handler.postDelayed({
            scannerManager.stopScan()
            handler.postDelayed(
                {
                    startScanTask()
                },
                5 * 1000
            )
        }, 15 * 1000)
    }
}